//
//  ContactCellView.m
//  Guazi
//
//  Created by koolearn on 11-5-8.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ContactCellView.h"
#import "Status.h"




@implementation ContactCellView

@synthesize user;
@synthesize searchText;
@synthesize isSearching;


- (id) init
{
    self = [super init];
    if ( self ) {
        isSearching = false;
    }
    return self;
}


- (void) setUser:(User *)u
{
	user = [u retain];
	[self setNeedsDisplay];
}


- (void) drawString:(NSString*)string withColor:(UIColor*)color searchColor:(UIColor*)searchColor inRect:(CGRect)rect withFont:(UIFont*)font
{
    NSString* part1 = string;
    NSString* part2 = @"";
    NSString* part3 = @"";
    
    if ( searchText && [searchText length] > 0 ) {
        NSRange range = [string rangeOfString:searchText];
        if ( range.location != NSNotFound ) {
            part1 = [string substringToIndex:range.location];
            part2 = searchText;
            NSInteger i = range.location + range.length;
            part3 = [string substringFromIndex:i];
        }
    }
    
    CGRect currentRect = rect;
    
    if ( [part1 length] > 0 ) {
        [color set];
        [part1 drawInRect:currentRect withFont:font];
        CGSize size = [part1 sizeWithFont:font];
        currentRect = CGRectMake(currentRect.origin.x + size.width, currentRect.origin.y, currentRect.size.width - size.width, currentRect.size.height);
    }
    
    if ( [part2 length] > 0  ) {
        [searchColor set];
        [part2 drawInRect:currentRect withFont:font];
        CGSize size = [part2 sizeWithFont:font];
        currentRect = CGRectMake(currentRect.origin.x + size.width, currentRect.origin.y, currentRect.size.width - size.width, currentRect.size.height);
    }
    
    if ( [part3 length] > 0 ) {
        [color set];
        [part3 drawInRect:currentRect withFont:font];
    }
}


#define kHeightScreenName 20
#define kHeightPhone 20
#define kHeightStatus 20
#define kHeightSeparator 5


- (void) drawRect:(CGRect)rect
{
    UIColor* textColor;
    UIColor* timestampColor;
	
	UITableViewCell* cell = (UITableViewCell*)self.superview.superview;
	if (cell.selected) {
		textColor       = [UIColor whiteColor];
        timestampColor  = [UIColor lightGrayColor];
	}
	else {
		textColor       = [UIColor blackColor];
        timestampColor  = [UIColor grayColor];
	}
    
    UIColor* searchColor = [UIColor redColor];
    
    float textFontSize = 13;
    CGFloat y = kHeightSeparator;
	
	[textColor set];
	//[user.screenName drawInRect:CGRectMake(0, y, CELL_WIDTH - DETAIL_BUTTON_WIDTH, TOP) withFont:[UIFont boldSystemFontOfSize:14]];
	//[user.screenName drawInRect:CGRectMake(0, y, CELL_WIDTH - DETAIL_BUTTON_WIDTH, 20) withFont:[UIFont boldSystemFontOfSize:19]];
    [self drawString:user.screenName withColor:textColor searchColor:searchColor 
              inRect:CGRectMake(0, y, CELL_WIDTH - DETAIL_BUTTON_WIDTH, kHeightScreenName) 
            withFont:[UIFont boldSystemFontOfSize:16]];
    y = y + kHeightScreenName + kHeightSeparator;
	
    
	if ( isSearching && user.phone != nil && [user.phone length]>0 ) {
        [self drawString:user.phone withColor:textColor searchColor:searchColor 
                  inRect:CGRectMake(0, y, CELL_WIDTH - DETAIL_BUTTON_WIDTH, kHeightPhone) 
                withFont:[UIFont boldSystemFontOfSize:14]];
        y = y + kHeightPhone + kHeightSeparator;
	}

	Status* status = user.status;
	if ( status != nil && status.text != nil && [status.text length] > 0 ) {
        
		NSString *firstText;
		int length = [status.text length];
		
		if ( length > 18 ) {
		    firstText = [ [status.text substringToIndex:16] stringByAppendingString:@"..."]; 
		}
		else {
			firstText = status.text;
        }
		
		CGRect rect = CGRectMake(0, y, status.textBounds.size.width, kHeightStatus );
		[firstText drawInRect:rect withFont:[UIFont systemFontOfSize:textFontSize]];
        
        y = y + kHeightStatus + kHeightSeparator;
		
		/*
		y = y + status.textBounds.size.height + 3;
		[timestampColor set];
		NSString *timestamp;
		if ([status.source length]) {
			timestamp = [status.timestamp stringByAppendingFormat:@" 来自%@", [status sourceDescription]];
		}
		else {
			timestamp = status.timestamp;
		}
		[timestamp drawInRect:CGRectMake(0, y, 250, 16) withFont:[UIFont systemFontOfSize:12]];
		 */
	}
}


+ (CGFloat) getViewHeight:(User*)auser isSearching:(BOOL)searching
{
    CGFloat height = kHeightScreenName;
	if ( searching && auser.phone != nil && [auser.phone length]>0 ) {
        height = height + kHeightPhone + kHeightSeparator;
    }
    
    Status* status = auser.status;
	if ( status != nil && status.text != nil && [status.text length] > 0 ) {
        height = height + kHeightStatus + kHeightSeparator;
    }
        
    height += 10;
    return height;
}


- (void) dealloc
{
	[user release];
    [searchText release];
	[super dealloc];
}

@end
